#include "arguments.h"

#include <windows.h>

namespace renderit
{
	static void _add_arg(std::map<std::string, std::string> & result, const char * p, int len)
	{
		const char * q = p;
		for (int i = 0; i < len; ++i) {
			if (p[i] == '=') {
				std::string key(p, i);
				std::string val(p + i + 1, len - i - 1);
				result[key] = val;
				return;
			}
		}
		std::string key(p, len);
		result[key] = "1";
	}

	std::map<std::string, std::string> get_arguments(int argc, const char * argv[])
	{
		std::map<std::string, std::string> result;

		const char * arguments = GetCommandLine();

		const char * p = nullptr;
		const char * q = arguments;
		for(; *q; ++q) {
			if (*q == ' ') {
				if (p) {
					_add_arg(result, p, q - p);
					p = nullptr;
				}
				continue;
			}
			if (!p) {
				p = q;
			}
		}
		if (p) {
			_add_arg(result, p, q - p);
		}
		return result;
	}
}